import csv
import re

# 创建工作链接
def output():
    clean_data()
    f1 = open("csv/boss.csv", "w", encoding='UTF-8')
    count = len(open("csv/work.csv", 'r', encoding='UTF-8').readlines())
    count1 = len(open("csv/decorate.csv", 'r', encoding='UTF-8').readlines())
    with open('csv/work.csv', 'r', encoding='UTF-8') as csvfile:
        reader = csv.reader(csvfile)
        if count1 > count:
            for i, row in enumerate(reader):
                if i < count - 1:
                    str1 = ''.join(row)
                    f1.write('https://www.zhipin.com/web/geek/job?query=' + str1 + '\n')  # 注意每写一行都需要换行
        else:
            for i, row in enumerate(reader):
                str1 = ''.join(row)
                f1.write('https://www.zhipin.com/web/geek/job?query=' + str1 + '\n')  # 注意每写一行都需要换行


# 处理大模型回答得到产品名字
def clean_data():
    clean_()

    pattern_str = '[^\u4e00-\u9fa5^a-z^A-Z^0-9]+'
    up_g = open("csv/work.csv", "w", encoding='UTF-8')

    with open('csv/decorate.csv', 'r', encoding='UTF-8') as csvfile:
        reader = csv.reader(csvfile)
        for i, rows in enumerate(reader):
            row = rows
            str1 = ''.join(row)
            a = str1.find("：")
            b = str1.find("，")
            c = str1.find("。")
            if str1.find("：") == -1:
                a = 1000
            if str1.find("，") == -1:
                b = 1000
            if str1.find("。") == -1:
                c = 1000

            if a < b and a < c:
                end_index = str1.find("：")
                new = str1[2:end_index]
                str_new = re.sub(pattern_str, '', new)
                up_g.write(str_new + '\n')  # 注意每写一行都需要换行
            elif b < a and b < c:
                end_index = str1.find("，")
                new = str1[2:end_index]
                str_new = re.sub(pattern_str, '', new)
                up_g.write(str_new + '\n')  # 注意每写一行都需要换行
            elif c < a and c < b:
                end_index = str1.find("。")
                new = str1[2:end_index]
                str_new = re.sub(pattern_str, '', new)
                up_g.write(str_new + '\n')  # 注意每写一行都需要换行


# 处理大模型答案得到四行数据
def clean_():
    count = len(open("csv/recommend.csv", 'r', encoding='UTF-8').readlines())
    data_new = open("csv/decorate.csv", "w", encoding='UTF-8')
    with open("csv/recommend.csv", "r", encoding='UTF-8') as f:
        data = csv.reader(f)  # 读取文本
        for i, rows in enumerate(data):
            if 1 < i < count - 2:
                row = rows
                str2 = ''.join(row)
                data_new.write(str2 + '\n')
    f.close()
